/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy;
import org.eclipse.gef.requests.CreateConnectionRequest;
import org.eclipse.gef.requests.ReconnectRequest;

import edu.pku.sei.modeler.commands.CreateLinkCommand;
import edu.pku.sei.modeler.edit.parts.AbstractNodeEditPart;
import edu.pku.sei.modeler.editpolicies.TemplateNodePolicy;
import edu.pku.sei.modeler.figures.TemplateNodeFigure;
import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.MetaLocationManager;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.structure.StructurePackage;

/**
 * @deprecated
 * @author zl
 *
 */
public class TemplateNodeEditPart extends AbstractNodeEditPart {
	public TemplateNodeEditPart (MetaShapeElement model) {
		super(model);
	}
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		Label header = new Label();
		TemplateNodeFigure fig = new TemplateNodeFigure(header);
		fig.setLayoutManager(new XYLayout());
		return fig;
	}

	public void activate() {
		super.activate();
//		List mgrs = getNode().getLocationMgrs();
//		for(int i=0; i<mgrs.size(); i++) {
//			MetaLocationManager mgr = (MetaLocationManager)mgrs.get(i);
//			mgr.addPropertyChangeListener(modelListener);
//		}
	}

	public void deactivate() {
//		List mgrs = getNode().getLocationMgrs();
//		for(int i=0; i<mgrs.size(); i++) {
//			MetaLocationManager mgr = (MetaLocationManager)mgrs.get(i);
//			mgr.removePropertyChangeListener(modelListener);
//		}
		super.deactivate();
	}
	
	protected DirectEditPolicy createDirectEditPolicy() {
		return null;
	}
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new GraphicalNodeEditPolicy() {
			protected Command getConnectionCompleteCommand(CreateConnectionRequest request) {
				CreateLinkCommand cmd = (CreateLinkCommand)request.getStartCommand();
				cmd.setTarget((MetaNode)getHost().getModel());
				System.out.println("connect complete");
				return cmd;
			}
			protected Command getConnectionCreateCommand(CreateConnectionRequest request) {
				Command cmd = new CreateLinkCommand((MetaLink)request.getNewObject(),
						(MetaNode)getHost().getModel());
				request.setStartCommand(cmd);
				System.out.println("connect create");
				return cmd;
			}
			protected Command getReconnectSourceCommand(ReconnectRequest request) {
				return UnexecutableCommand.INSTANCE;
			}
			protected Command getReconnectTargetCommand(ReconnectRequest request) {
				return UnexecutableCommand.INSTANCE;
			}
		});
		installEditPolicy(EditPolicy.LAYOUT_ROLE,new TemplateNodePolicy());
	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		if(propertyName.equals(StructurePackage.LOCATIONMANAGER_NODES)) {
			refreshVisuals();
		}
		else if(event.getPropertyName().equals(StructurePackage.NODE_SUBNODES)) {
			refreshChildren();
		}
		super.handlePropertyChanged(event);
	}
	
//	private TemplateNode getTemplateNode() {
//		return (TemplateNode)((MetaNode)getModel()).getElement();
//	}
	
	protected List getModelChildren() {
		List children=new ArrayList();
		children.addAll(((MetaNode)getModel()).getSubNodes());
		return children;
	}
	
	protected void refreshVisuals() {
		super.refreshVisuals();		
//		Label header = (Label)((TemplateNodeFigure)getFigure()).getHeader();
//		header.setText(getTemplateNode().getName());
//		getFigure().setBackgroundColor(new Color(null,230,230,230));
//		MetaNode node = getNode();
//		int x = node.getLocationX();
//		int y = node.getLocationY();
//		int width = node.getWidth();
//		int height = node.getHeight();
//		Rectangle r = new Rectangle(x, y, width, height);
//		List mgrs = node.getLocationMgrs();
//		for(int i=0; i<mgrs.size(); i++) {
//			MetaLocationManager mgr = (MetaLocationManager)mgrs.get(i);
//			mgr.locateSubParts(r);
//		}
	}

}
